package tw.org.iii.dataaccesssample;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ActToDo extends Activity {

	ArrayList<CToDoItem> listItem =new ArrayList<CToDoItem>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_acttodo);
		InitialComponent();
	}
	OnClickListener btnAdd_click=new OnClickListener(){
		public void onClick(View arg0) {
			
			SharedPreferences table = getSharedPreferences("TTODO", 0);
			int intMax=table.getInt("K_COUNT",0);
			intMax++;			
			table.edit().putInt("K_COUNT", intMax).commit();
			String strKDate="KDATE"+String.valueOf(intMax);
			String strKTODO="KTODO"+String.valueOf(intMax);
			table.edit().putString(strKDate, txtDate.getText().toString()).commit();
			table.edit().putString(strKTODO, txtToDo.getText().toString()).commit();
			
			listItem.add(new CToDoItem(intMax, txtToDo.getText().toString(), txtDate.getText().toString()));
			
			Toast.makeText(ActToDo.this, "儲存資料成功", Toast.LENGTH_SHORT).show();
	}};
	
	DialogInterface.OnClickListener btnSelect_Click=new DialogInterface.OnClickListener(){

		@Override
		public void onClick(DialogInterface arg0, int arg1) {
			// TODO Auto-generated method stub
			
		}};
	OnClickListener btnBrowse_click=new OnClickListener(){
		public void onClick(View arg0) {
			
			String[] strTodo=new String[listItem.size()];
			for(int i=0;i<strTodo.length;i++){
				strTodo[i]=listItem.get(i).GetToDo();
			}
			
			
			Builder l_build=new Builder(ActToDo.this);
			l_build.setTitle("未完成事件");			
			l_build.setItems(strTodo,btnSelect_Click);
			l_build.show();


	}};
	
	private void InitialComponent() {
		
		btnAdd=(Button)findViewById(R.id.btnAdd);
		btnAdd.setOnClickListener(btnAdd_click);
		btnBrowse=(Button)findViewById(R.id.btnBrowse);
		btnBrowse.setOnClickListener(btnBrowse_click);
		txtDate=(EditText)findViewById(R.id.txtDate);
		txtToDo=(EditText)findViewById(R.id.txtToDo);
	
	}
   
	Button btnAdd=null;
	Button btnBrowse=null;
	EditText txtDate=null;
	EditText txtToDo=null;

}
